home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug231 / main.c < prev    next >
Text File  |  1987-06-17  |  9KB  |  347 lines

  1. /*
  2.     Little Smalltalk -
  3.         main driver
  4.  
  5.         timothy a. budd
  6.  
  7. 1.     initializes various smalltalk constants and classes with
  8.     legitimate values.  these values, however, will for the most part
  9.     be overridden when the standard prelude is read in.
  10.  
  11. 2.    reads in the standard prelude, plus any additional files listed
  12.     on the command line.
  13.  
  14. 3.    places the driver reading stdin on the process queue and starts
  15.     the process driver running.
  16. */
  17. /*
  18.     The source code for the Little Smalltalk System may be freely
  19.     copied provided that the source of all files is acknowledged
  20.     and that this condition is copied with each file.
  21.  
  22.     The Little Smalltalk System is distributed without responsibility
  23.     for the performance of the program and without any guarantee of
  24.     maintenance.
  25.  
  26.     All questions concerning Little Smalltalk should be addressed to:
  27.  
  28.         Professor Tim Budd
  29.         Department of Computer Science
  30.         Oregon State University
  31.         Corvallis, Oregon
  32.         97331
  33.         USA
  34. */
  35.  
  36. int version = 2; /* a Kludge to get us the start of the data segment.
  37.             used to save and restore contexts */
  38.  
  39.  
  40. # include <stdio.h>
  41. # include "object.h"
  42. # include "string.h"
  43. # include "symbol.h"
  44. # include "interp.h"
  45. # include "primitive.h"
  46.  
  47. static object *null_object;    /* a totally classless object */
  48. static char filebase[80];    /* base for forming temp file names */
  49.  
  50. extern int n_incs, n_decs, n_mallocs;    /* counters */
  51. extern int opcount[], ohcount, spcount[];
  52.  
  53. extern int ca_block, ca_barray, ca_class, ca_terp, ca_int, ca_float;
  54. extern int ca_obj, ca_str, ca_sym, ca_wal, ca_cdict;
  55. extern int ca_cobj[];
  56. extern int btabletop, wtop;    /* more counters */
  57.  
  58. # ifdef INLINE
  59. object *_dx;        /* object pointer used for decrementing */
  60. # endif
  61.  
  62. int silence = 0;        /* 1 if silence is desired on output */
  63. int noload = 0;         /* 1 if no loading of standard prelude is desired */
  64. int debug = 0;        /* debug flag, set by a primitive call */
  65. int fastload = 0;    /* 1 if doing a fast load of saved image */
  66. int lexprnt = 0;    /* 1 if printing during lex is desired (for debug) */
  67. int prallocs = 0;    /* 1 if printing final allocation figures is wanted */
  68. int started = 0;    /* 1 if we have started reading user commands */
  69. int prntcmd = 1;    /* 1 or 2 and commands will be printed as evaled */
  70.  
  71. /* pseudo-variables */
  72. object *o_acollection;        /* arrayed collection (used internally) */
  73. object *o_drive;        /* driver interpreter */
  74. object *o_empty;        /* the empty array (used during initial) */
  75. object *o_false;        /* value for pseudo variable false */
  76. object *o_magnitude;        /* instance of class Magnitude */
  77. object *o_nil;            /* value for pseudo variable nil */
  78. object *o_number;        /* instance of class Number */
  79. object *o_object;        /* instance of class Object */
  80. object *o_tab;            /* string with tab only */
  81. object *o_true;            /* value of pseudo variable true */
  82. object *o_smalltalk;        /* value of pseudo variable smalltalk */
  83.  
  84. /* classes to be initialized */
  85. extern class *Array;
  86. extern class *ArrayedCollection;
  87.  
  88. /* input stack */
  89. extern FILE *fdstack[];
  90. extern int fdtop;
  91.  
  92. /* main - main driver */
  93. main(argc, argv)
  94. int argc;
  95. char **argv;
  96. {    int i;
  97.     class *null_class();
  98.     object *tempobj;
  99.     FILE *sfd;
  100.  
  101. # ifdef FASTDEFAULT
  102.     fastload = 1;
  103. # endif
  104. # ifndef FASTDEFAULT
  105.     fastload = 0;
  106. # endif
  107.  
  108.     /* first check for flags */
  109.     for (i = 1; i < argc; i++)
  110.         if (argv[i][0] == '-')
  111.             switch(argv[i][1]) {
  112.                 case 'f': fastload = 1; break;
  113.                 case 'l':         /* fall through */
  114.                 case 'n': noload = 1; /* fall through */
  115.                 case 'm': fastload = 0; break;
  116.                 case 'z': lexprnt = 1; break;
  117.             }
  118.  
  119.     if (fastload) {
  120.         dofast();
  121.         }
  122.     else {            /* gotta do it the hard way */
  123.         strcpy(filebase, TEMPFILE);
  124.         mktemp(filebase);
  125.  
  126.         byte_init();
  127.         class_init();
  128.         cdic_init();
  129.         int_init();
  130.         str_init();
  131.         sym_init();
  132.         init_objs();
  133.  
  134.         null_object = new_obj((class *) 0, 0, 0);
  135.  
  136.         sassign(o_object, null_object);
  137.         /* true is given a different object from others , so comparisons
  138.                     work correctly */
  139.         sassign(o_true, new_obj((class *) 0, 0, 0));
  140.         sassign(o_false, null_object);
  141.         sassign(o_nil, null_object);
  142.         sassign(o_number, null_object);
  143.         sassign(o_magnitude, null_object);
  144.         sassign(o_empty, null_object);
  145.         sassign(o_smalltalk, null_object);
  146.         sassign(o_acollection, null_object);
  147.  
  148.         sassign(Array, null_class("Array"));
  149.         sassign(ArrayedCollection, null_class("ArrayedCollection"));
  150.  
  151.         drv_init();    /* initialize the driver */
  152.         sassign(o_drive, (object *) cr_interpreter((interpreter *) 0,
  153.             null_object, null_object, null_object, null_object));
  154.         init_process((interpreter *) o_drive);
  155.  
  156.         /* now read in standard prelude */
  157.         if (! noload) {
  158.             sfd = fopen(PRELUDE, "r");
  159.             if (sfd == NULL) cant_happen(20);
  160.             set_file(sfd);
  161.             start_execution();
  162.             fclose(sfd);
  163.             }
  164.  
  165.         /* then set lexer up to read stdin */
  166.         set_file(stdin);
  167.         sassign(o_tab, new_str("\t"));
  168.  
  169. # ifdef CURSES
  170.         /* finally initialize the curses window package */
  171.         initscr();
  172. # endif
  173. # ifdef PLOT3
  174.         /* initialize the plotting device */
  175.         openpl();
  176. # endif
  177.         }
  178.  
  179.     /* announce that we're ready for action */
  180.     sassign(tempobj, new_sym("Little Smalltalk"));
  181.     primitive(SYMPRINT, 1, &tempobj);
  182.     obj_dec(tempobj);
  183.     started = 1;
  184.  
  185.     /* now read in the command line files */
  186.     user_read(argc, argv);
  187.  
  188.     start_execution();
  189.  
  190.     /* print out one last newline - to move everything out of output
  191.     queue */
  192.     sassign(tempobj, new_sym("\n"));
  193.     primitive(SYMPRINT, 1, &tempobj);
  194.     obj_dec(tempobj);
  195.  
  196.     /* now free things up, hopefully keeping ref counts straight */
  197.  
  198.     drv_free();
  199.  
  200.     flush_processes();
  201.  
  202.     free_low_nums();
  203.  
  204.     obj_dec((object *) Array);
  205.     obj_dec((object *) ArrayedCollection);
  206.  
  207.     free_all_classes();
  208.     
  209.     obj_dec(o_tab);
  210.     obj_dec(o_drive);
  211.     obj_dec(o_magnitude);
  212.     obj_dec(o_number);
  213.     obj_dec(o_nil);
  214.     obj_dec(o_false);
  215.     obj_dec(o_true);
  216.     obj_dec(o_object);
  217.     obj_dec(o_empty);
  218.     obj_dec(o_smalltalk);
  219.     obj_dec(o_acollection);
  220.  
  221.     if (! silence)
  222.         fprintf(stderr,"incs %u decs %u difference %d allocs %d\n",
  223.             n_incs, n_decs, n_incs - n_decs, n_mallocs);
  224.     ohcount = 0;
  225.     for (i = 0; i < 16; i++)
  226.         ohcount += opcount[i];
  227.     fprintf(stderr,"opcount %d\n", ohcount);
  228.         /*fprintf(stderr,"opcode [%d] counts %d\n", i, opcount[i]);*/
  229.     /*fprintf(stderr,"ohcount %d\n", ohcount);
  230.     for (i = 0; i < 16; i++)
  231.         fprintf(stderr,"sp count %d %d\n", i , spcount[i]);*/
  232.     if (prallocs) {
  233.         fprintf(stderr,"blocks allocated %d\n", ca_block);
  234.         fprintf(stderr,"bytearrays allocated %d\n", ca_barray);
  235.         fprintf(stderr,"classes allocated %d\n", ca_class);
  236.         fprintf(stderr,"interpreters allocated %d\n", ca_terp);
  237.         fprintf(stderr,"ints allocated %d\n", ca_int);
  238.         fprintf(stderr,"floats allocated %d\n", ca_float);
  239.         fprintf(stderr,"strings allocated %d\n", ca_str);
  240.         fprintf(stderr,"symbols allocated %d\n", ca_sym);
  241.         fprintf(stderr,"class entryies %d\n", ca_cdict);
  242.         fprintf(stderr,"wallocs %d\n", ca_wal);
  243.         fprintf(stderr,"wtop %d\n", wtop);
  244.         fprintf(stderr,"byte table top %d\n", btabletop);
  245.         fprintf(stderr,"smalltalk objects allocated %d\n", ca_obj);
  246.         for (i = 0; i < 5; i++)
  247.             fprintf(stderr,"size %d objects %d\n", i, ca_cobj[i]);
  248.     }
  249.     clean_files();
  250.  
  251. # ifdef PLOT3
  252.     closepl();
  253. # endif
  254. # ifdef CURSES
  255.     endwin();
  256. # endif
  257.  
  258.     exit(0);    /* say good by gracie */
  259. }
  260.  
  261. /* dofast - do a fast load of the standard prelude */
  262. static dofast() {
  263.     char buffer[100];
  264.  
  265.     sprintf(buffer,")l %s\n", FAST);
  266.     dolexcommand(buffer);
  267. }
  268.  
  269. /* null_class - create a null class for bootstrapping purposes */
  270. static class *null_class(name)
  271. char *name;
  272. {    class *new, *new_class();
  273.  
  274.     new = new_class();
  275.     assign(new->class_name, new_sym(name));
  276.     enter_class(name, (object *) new);
  277.     return(new);
  278. }
  279.  
  280. /* user_read - read the user command line arguments */
  281. static user_read(argc, argv)
  282. int argc;
  283. char **argv;
  284. {    int i, count;
  285.     char c, buffer[100];
  286.     char name[100];
  287.     FILE *fd = 0;
  288.  
  289.     gettemp(name);
  290.     count = 0;
  291.     fd = fopen(name, "w");
  292.     if (fd == NULL)
  293.         cant_happen(22);
  294.     for (i = 1; i < argc; i++)
  295.         if (argv[i][0] == '-') {
  296.             switch(argv[i][1]) {
  297.                 case 'a':
  298.                     prallocs = 1; break;
  299.                 case 'g': case 'l': case 'r':
  300.                     c = argv[i][1];
  301.                     sprintf(buffer,")%c %s\n",
  302.                         c, argv[++i]);
  303.                     count++;
  304.                     fputs(buffer, fd);
  305.                     break;
  306.                 case 'd':
  307.                     prntcmd = argv[i][1] - '0';
  308.                     break;
  309.                 case 's':
  310.                     silence = 1;
  311.                     break;
  312.                 }
  313.             }
  314.         else {
  315.             sprintf(buffer,")i %s\n", argv[i]);
  316.             count++;
  317.             fputs(buffer, fd);
  318.             }
  319.     fclose(fd);
  320.     if (count) {
  321.         fd = fopen(name, "r");
  322.         if (fd == NULL)
  323.             cant_happen(22);
  324.         set_file(fd);
  325.         }
  326. }
  327.  
  328. /* gettemp makes a temp file name that can be deleted when finished */
  329. static char c = 'a';
  330. gettemp(buffer)
  331. char *buffer;
  332. {
  333.     sprintf(buffer,"%s%c", filebase, c++);
  334.     if (c > 'z') c = 'a';    /* wrap around forever */
  335. }
  336.  
  337. /* clean_files - delete all temp files created */
  338. static clean_files()
  339. {
  340.     char buffer[100];
  341.  
  342. # ifndef NOSYSTEM
  343.     sprintf(buffer,"rm -f %s*", filebase);
  344.     system(buffer);
  345. # endif
  346. }
  347.